require('libraries/notifications')
if resource == nil then
	resource = {
	player_reputation = {},
	player_killed_num ={},
	player_cur_official = {},
	official_table = {
		{killed = 10,gold = 100},
		{killed = 42,gold = 200},
		{killed = 93,gold = 300},
		{killed = 150,gold = 400},
		{killed = 375,gold = 500},
		{killed = 550,gold = 600},
		{killed = 750,gold = 700},
		{killed = 1000,gold = 800},
		{killed = 1300,gold = 900},
		{killed = 1650,gold = 1000},
		{killed = 2075,gold = 1100},
		{killed = 2500,gold = 1200},
		{killed = 3250,gold = 1300},
		{killed = 4000,gold = 1400}
	},
	player_incoming = {},
	player_max_food = {},
	player_cur_food = {},
	player_wood = {},
	player_bag = {},
	}
	last_incoming_time = 0
end
function resource:init()
	
	 for i =0,7 do
	 	--初始化玩家声望
	 	self.player_reputation[i] = 0
	 	--初始化杀敌数
	 	self.player_killed_num[i] = 0
	 	--初始化当前官职
	 	self.player_cur_official[i] = 0
	 	--初始化当前收入
	 	self.player_incoming[i] = 25
	 	--初始化最大人口
	 	self.player_max_food[i] = 15
	 	--初始化当前人口
	 	self.player_cur_food[i] = 0
	 	--初始化木头
	 	self.player_wood[i] = 0
	 	--初始化当前宝物栏
	 	self.player_bag[i] = {}
	 end
	 --监听杀敌数 
	 ListenToGameEvent( "entity_killed",Dynamic_Wrap(  resource,"OnKilled"), self )
	 local GameMode = GameRules:GetGameModeEntity() 
	GameMode:SetContextThink(DoUniqueString("IncomingThink"), self.IncomingThink,0)
end
function resource:IncomingThink()
	if not GameRules:IsGamePaused() then 
		if GameRules:GetGameTime() - last_incoming_time >= 10 then --超过了给予收入时间
			last_incoming_time = GameRules:GetGameTime()
			for i = 0,7 do
				PlayerResource:ModifyGold(i,resource.player_incoming[i], false, 0) --
				--Notifications:Top(i, {text="#incoming", duration=3, continue=true})
				--UTIL_MessageText_WithContext(i, "#incoming_info", 255, 255,255,10, {value = resource.player_incoming[i]}) --提醒当前收入
			end
		end
		return 0.1
	end
end
function resource:OnKilled(event)
	local target = EntIndexToHScript(event.entindex_killed) 
	local target_name = target:GetUnitName()
	local attacker = nil
	if event.entindex_attacker ~= nil then 
	    attacker = EntIndexToHScript(event.entindex_attacker) 
    else
    	return
    end
    print("OnKilled")
    local PlayerID = attacker:GetPlayerOwnerID()
    local hero = PlayerResource:GetPlayer(PlayerID):GetAssignedHero()
    self.player_killed_num[PlayerID] = self.player_killed_num[PlayerID] + 1
    print("self.player_killed_num[PlayerID]:",self.player_killed_num[PlayerID])
    if self.player_killed_num[PlayerID] >= self.official_table[self.player_cur_official[PlayerID]+1]["killed"]  then
    	self.player_cur_official[PlayerID] = self.player_cur_official[PlayerID] + 1 -- 升官
    	hero:ModifyGold(self.official_table[self.player_cur_official[PlayerID]]["gold"],false,0)--加钱
    	self.player_reputation[PlayerID] = self.player_reputation[PlayerID] + 2 --加声望
    	--local info = string.format("%s%s%d","#","level_up_info_",self.player_cur_official[PlayerID]) -- 这个地方写信息
	 	--GameRules:SendCustomMessage(info, 0,0) 
	 	print("playerID:",PlayerID,
	 		"cur_level:",self.player_cur_official[PlayerID],
	 		"cur_reputation:",self.player_reputation[PlayerID],
	 		"killed_num:",self.player_killed_num[PlayerID])
    end
end
function resource:RollItem(PlayerID)
	local f = RandomInt(0,100)
	if f > self.player_reputation[PlayerID] then return false end
	return true
end
function resource:OnEquipItem(PlayerID,item,slot)
	player_bag[slot] = item
end
function resource:GetPlayerReputation(PlayerID)
	return self.player_reputation[PlayerID]
end